package com.example.thread;

public class SynchronizedTest {

    public static void main(String[] args){
        Syn syn = new Syn();
        new MyThread(syn).start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new MyThread1(syn).start();
    }

    static class MyThread1 extends Thread{
        private Syn syn;
        MyThread1(Syn syn){
            this.syn = syn;
        }

        @Override
        public void run() {
            syn.method2();
        }
    }

    static class MyThread extends Thread{
        private Syn syn;
        MyThread(Syn syn){
            this.syn = syn;
        }

        @Override
        public void run() {
            syn.method1();
        }
    }

    static class Syn{
        static synchronized void method1(){
            System.out.println(Thread.currentThread().getName() + "in Class method1");
            try {

                Thread.sleep(500000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //while (true){}

        }

         void  method2(){
            synchronized(Syn.class){
                System.out.println(Thread.currentThread().getName() + "in instance method2");
            }
        }
    }
}
